package com.timebank.timecoin.controller;

import com.timebank.common.core.result.Result;
import com.timebank.common.core.web.controller.BaseController;
import com.timebank.common.core.web.page.TableDataInfo;
import com.timebank.common.core.web.pojo.AjaxResult;
import com.timebank.common.log.annotation.Log;
import com.timebank.common.log.enums.BusinessType;
import com.timebank.common.security.annotation.InnerAuth;
import com.timebank.common.security.annotation.RequiresLogin;
import com.timebank.timecoin.pojo.dto.TimeCoinTradeDTO;
import com.timebank.timecoin.pojo.entity.TimeCoinTrade;
import com.timebank.timecoin.pojo.vo.TimeCoinTradeVO;
import com.timebank.timecoin.service.TimeCoinTradeService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.hyperledger.fabric.gateway.ContractException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.openfeign.SpringQueryMap;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;
import java.util.concurrent.TimeoutException;

/**
 * @author shiliuyinzhen
 */
@RestController
@RequestMapping("/trade")
@Api(tags = "时间币交易相关接口")
public class TimeCoinTradeController extends BaseController {

    @Autowired
    private TimeCoinTradeService timeCoinTradeService;

    /**
     * 获取时间币支出交易列表
     * @param timeCoinTradeDTO
     * @return
     */
    @RequiresLogin
    @GetMapping("/list/pay")
    @ApiOperation("获取时间币支出交易列表")
    public TableDataInfo getPayTradeByUserId(TimeCoinTradeDTO timeCoinTradeDTO){
        startPage();
        List<TimeCoinTradeVO> list = timeCoinTradeService.getPayTradeByUserId(timeCoinTradeDTO);
        return getDataTable(list);
    }

    /**
     * 获取时间币收入交易列表
     * @return
     */
    @RequiresLogin
    @GetMapping("/list/get")
    @ApiOperation("获取时间币收入交易列表")
    public TableDataInfo getGetTradeByUserId(TimeCoinTradeDTO timeCoinTradeDTO){
        startPage();
        List<TimeCoinTradeVO> list = timeCoinTradeService.getGetTradeByUserId(timeCoinTradeDTO);
        return getDataTable(list);
    }

    /**
     * 获取时间币所有交易列表
     * @return
     */
    @RequiresLogin
    @GetMapping("/list")
    @ApiOperation("获取时间币所有交易列表")
    public TableDataInfo getAllTradeByUserId(TimeCoinTradeDTO timeCoinTradeDTO){
        startPage();
        List<TimeCoinTradeVO> list = timeCoinTradeService.getAllTradeByUserId(timeCoinTradeDTO);
        return getDataTable(list);
    }

    /**
     * 创建时间币交易
     * @param timeCoinTrade
     * @return
     */
    @InnerAuth
    @Log(title = "时间币服务" ,businessType = BusinessType.INSERT)
    @PostMapping
    @ApiOperation("创建时间币交易")
    public AjaxResult createTrade(@RequestBody TimeCoinTrade timeCoinTrade){
        return toAjax(timeCoinTradeService.createTrade(timeCoinTrade));
    }

    /**
     * 修改时间币交易状态
     * @param tradeId
     * @return
     */
    @InnerAuth
    @PutMapping("/status/{tradeId}")
    @ApiOperation("修改时间币交易状态")
    public AjaxResult updateTradeStatus(@PathVariable Long tradeId) throws ContractException, InterruptedException, TimeoutException {
        return toAjax(timeCoinTradeService.updateTradeStatus(tradeId));
    }

    /**
     * 批量删除时间币交易
     * @param tradeIds
     * @return
     */
    @InnerAuth
    @Log(title = "时间币服务" ,businessType = BusinessType.DELETE)
    @DeleteMapping("/{tradeIds}")
    @ApiOperation("批量删除时间币交易")
    public AjaxResult deleteTradeByIds(@PathVariable Long[] tradeIds){
        return toAjax(timeCoinTradeService.deleteTradeByIds(tradeIds));
    }

    /**
     * 根据志愿服务Id和获得者Id删除时间币交易
     * @param map
     * @return
     */
    @InnerAuth
    @Log(title = "时间币服务" ,businessType = BusinessType.DELETE)
    @DeleteMapping
    @ApiOperation("根据志愿服务Id和获得者Id删除时间币交易")
    public AjaxResult deleteTradeByVolunteerIdAndGetterId(@RequestBody Map<String,Long> map){
        Long volunteerId = map.get("volunteerId");
        Long getterId = map.get("getterId");
        return toAjax(timeCoinTradeService.deleteTradeByVolunteerIdAndGetterId(volunteerId,getterId));
    }

    /**
     * 根据志愿服务Id支付时间币
     * @param volunteerId
     * @return
     */
    @InnerAuth
    @PostMapping("/pay/{volunteerId}")
    @ApiOperation("根据志愿服务Id支付时间币")
    public AjaxResult payTimeCoinByVolunteerId(@PathVariable("volunteerId") Long volunteerId) throws ContractException, InterruptedException, TimeoutException {
        return timeCoinTradeService.payTimeCoinByVolunteerId(volunteerId);
    }

    /**
     * 根据志愿服务Ids批量删除时间币交易
     * @param volunteerIds
     * @return
     */
    @InnerAuth
    @Log(title = "时间币服务", businessType = BusinessType.DELETE)
    @DeleteMapping("/volunteer/{volunteerIds}")
    @ApiOperation("根据志愿服务Ids批量删除时间币交易")
    AjaxResult deleteTradeByVolunteerIds(@PathVariable("volunteerIds")Long[] volunteerIds){
        return toAjax(timeCoinTradeService.deleteTradeByVolunteerIds(volunteerIds));
    }

    /**
     * 根据用户Ids和志愿服务Id批量删除时间币交易
     * @param userIds
     * @return
     */
    @InnerAuth
    @Log(title = "时间币服务", businessType = BusinessType.DELETE)
    @DeleteMapping("/user/{userIds}")
    @ApiOperation("根据用户Ids和志愿服务Id批量删除时间币交易")
    AjaxResult deleteTradeByVolunteerIdsAndVolunteerId(@PathVariable("userIds")Long[] userIds, @RequestBody Long volunteerId){
        return toAjax(timeCoinTradeService.deleteTradeByUserIdsAndVolunteerId(userIds,volunteerId));
    }

    /**
     * 条件查询时间币交易
     * @return
     */
    @InnerAuth
    @GetMapping("/query")
    @ApiOperation("条件查询时间币交易")
    public Result<List<TimeCoinTradeVO>> getTrade(@SpringQueryMap TimeCoinTrade timeCoinTrade){
        return Result.success(timeCoinTradeService.getTradeByCondition(timeCoinTrade));
    }
}
